package org.niux.AutoLoginQQ.ui;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.wb.swt.SWTResourceManager;
import org.niux.AutoLoginQQ.common.Constant;
import org.niux.AutoLoginQQ.util.FileOperate;

public class ExportSetter extends Dialog {

	protected Object result;
	protected Shell shell;
	private Button selectAllBtn;
	private Button noCheck;
	private Button qqNumCheck;
	private Button passwordCheck;
	private Button nickCheck;
	private Button levelCheck;
	private Button onlineCheck;
	private Button remainCheck;
	private Table table;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public ExportSetter(Shell parent, int style,Table table) {
		super(parent, style);
		this.table = table;
		setText("导出列表设置");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();

		// 窗口居中
		int width = shell.getMonitor().getClientArea().width;
		int height = shell.getMonitor().getClientArea().height;

		int x = shell.getSize().x;
		int y = shell.getSize().y;

		if (x > width) {
			shell.getSize().x = width;
		}

		if (y > height) {
			shell.getSize().y = height;
		}

		shell.setLocation((width - x) / 2, (height - y) / 2);

		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM);
		shell.setSize(357, 199);

		shell.setBackground(SWTResourceManager.getColor(173, 216, 230));
		shell.setText("导出列表设置");

		selectAllBtn = new Button(shell, SWT.CHECK);
		selectAllBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(selectAllBtn.getSelection()){
					if(!noCheck.getSelection()){
						noCheck.setSelection(true);
					}
					if(!qqNumCheck.getSelection()){
						qqNumCheck.setSelection(true);
					}
					if(!passwordCheck.getSelection()){
						passwordCheck.setSelection(true);
					}
					if(!nickCheck.getSelection()){
						nickCheck.setSelection(true);
					}
					if(!levelCheck.getSelection()){
						levelCheck.setSelection(true);
					}
					if(!onlineCheck.getSelection()){
						onlineCheck.setSelection(true);
					}
					if(!remainCheck.getSelection()){
						remainCheck.setSelection(true);
					}
				} else {
					if(noCheck.getSelection()){
						noCheck.setSelection(false);
					}
					if(qqNumCheck.getSelection()){
						qqNumCheck.setSelection(false);
					}
					if(passwordCheck.getSelection()){
						passwordCheck.setSelection(false);
					}
					if(nickCheck.getSelection()){
						nickCheck.setSelection(false);
					}
					if(levelCheck.getSelection()){
						levelCheck.setSelection(false);
					}
					if(onlineCheck.getSelection()){
						onlineCheck.setSelection(false);
					}
					if(remainCheck.getSelection()){
						remainCheck.setSelection(false);
					}
				}
			}
		});
		selectAllBtn.setBounds(10, 10, 61, 17);
		selectAllBtn.setText("全选");
		selectAllBtn.setBackground(SWTResourceManager.getColor(173, 216, 230));

		Group group = new Group(shell, SWT.NONE);
		group.setText("请选择需要导出的列");
		group.setBounds(10, 33, 331, 78);
		group.setBackground(SWTResourceManager.getColor(173, 216, 230));

		noCheck = new Button(group, SWT.CHECK);
		noCheck.setBounds(10, 22, 65, 17);
		noCheck.setText("序号");
		noCheck.setBackground(SWTResourceManager.getColor(173, 216, 230));

		qqNumCheck = new Button(group, SWT.CHECK);
		qqNumCheck.setBackground(SWTResourceManager.getColor(173, 216, 230));
		qqNumCheck.setBounds(81, 22, 76, 17);
		qqNumCheck.setText("QQ号码");

		passwordCheck = new Button(group, SWT.CHECK);
		passwordCheck.setBackground(SWTResourceManager.getColor(173, 216, 230));
		passwordCheck.setBounds(163, 22, 76, 17);
		passwordCheck.setText("密码");

		nickCheck = new Button(group, SWT.CHECK);
		nickCheck.setBackground(SWTResourceManager.getColor(173, 216, 230));
		nickCheck.setBounds(245, 22, 70, 17);
		nickCheck.setText("昵称");

		levelCheck = new Button(group, SWT.CHECK);
		levelCheck.setBackground(SWTResourceManager.getColor(173, 216, 230));
		levelCheck.setBounds(10, 45, 65, 17);
		levelCheck.setText("等级");

		onlineCheck = new Button(group, SWT.CHECK);
		onlineCheck.setBackground(SWTResourceManager.getColor(173, 216, 230));
		onlineCheck.setBounds(81, 45, 76, 17);
		onlineCheck.setText("活跃天数");

		remainCheck = new Button(group, SWT.CHECK);
		remainCheck.setBackground(SWTResourceManager.getColor(173, 216, 230));
		remainCheck.setBounds(163, 45, 76, 17);
		remainCheck.setText("剩余天数");

		Button exportBtn = new Button(shell, SWT.NONE);
		exportBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(!noCheck.getSelection() && !qqNumCheck.getSelection() && !passwordCheck.getSelection() && !nickCheck.getSelection() &&
						!levelCheck.getSelection() && !onlineCheck.getSelection() && !remainCheck.getSelection()){
					alert("至少需要选择一个导出项");
					return;
				}
				
				TableItem[] tableItems = table.getItems();
				
				int selectCount = 0;
				
				for (TableItem tableItem : tableItems) {
					if(tableItem.getChecked()){
						selectCount++;
					}
				}
				
				List<String> exportList = new ArrayList<String>();
				
				if(selectCount == 0){
					alert("请选择需要导出的QQ号码");
					return;
				} else {
					for (int i = 0; i < table.getItemCount();i++) {
						
						StringBuffer exportString = new StringBuffer();
						
						if(table.getItem(i).getChecked()){
							if(noCheck.getSelection()){
								exportString.append(table.getItem(i).getText(1) + Constant.SPLIT_STR);
							} 
							if(qqNumCheck.getSelection()){
								exportString.append(table.getItem(i).getText(2) + Constant.SPLIT_STR);
							} 
							if(passwordCheck.getSelection()){
								exportString.append(table.getItem(i).getText(3) + Constant.SPLIT_STR);
							} 
							if(nickCheck.getSelection()){
								if(!table.getItem(i).getText(5).equals("")){
									exportString.append(table.getItem(i).getText(5) + Constant.SPLIT_STR);
								}
							} 
							if(levelCheck.getSelection()){
								if(!table.getItem(i).getText(5).equals("")){
									exportString.append("等级："+table.getItem(i).getText(6) + Constant.SPLIT_STR);
								}
							} 
							if(onlineCheck.getSelection()){
								if(!table.getItem(i).getText(5).equals("")){
									exportString.append("活跃天数："+table.getItem(i).getText(7) + Constant.SPLIT_STR);
								}
							} 
							if(remainCheck.getSelection()){
								if(!table.getItem(i).getText(5).equals("")){
									exportString.append("剩余天数："+table.getItem(i).getText(8) + Constant.SPLIT_STR);
								}
							}
							
							exportString = new StringBuffer(exportString.substring(0, exportString.length() - Constant.SPLIT_STR.length()));
							exportString.append("\r\n");
						}
						exportList.add(exportString.toString());
					}
				}
				
				FileDialog dialog = new FileDialog (shell, SWT.NONE);
				dialog.setText("导出QQ列表...");
				dialog.setFilterNames (new String [] {"文本文件", "文本文件 (*.txt)"});
				dialog.setFilterExtensions (new String [] {"*.txt"});
				String path = dialog.open();
				
				if(path != null){
					File tempFile = new File(path);
					String fileName = tempFile.getName();
					
					String[] fileNameArray = fileName.split("\\.");
					
					if(!fileNameArray[fileNameArray.length - 1].equals("txt")){
						fileName += ".txt";
					}
					
					path = tempFile.getParent() + "\\" + fileName;
					
					File file = new File(path);
					
					if(file.exists()){
						boolean flag = confirm("文件已经存在，是否覆盖原文件？");
						
						if(flag){
							FileOperate fo = new FileOperate();
							fo.write(file, exportList);
							alert("QQ列表导出成功");
						} else {
							alert("请点击导出QQ列表按钮并输入新的文件名");
						}
					} else {
						try{
							if (!file.createNewFile()) {
								alert("文件："+path+"创建失败，请确定您的登录帐号有写文件的权限");
							} else {
								FileOperate fo = new FileOperate();
								fo.write(file, exportList);
								alert("QQ列表导出成功");
							}
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
				}
				
				shell.close();
			}
		});
		exportBtn.setBounds(68, 123, 80, 27);
		exportBtn.setText("导出");

		Button cancleBtn = new Button(shell, SWT.NONE);
		cancleBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		cancleBtn.setBounds(197, 123, 80, 27);
		cancleBtn.setText("取消");
	}

	/**
	 * 
	 * 方法概述：提示信息对话框 
	 * 描述：提示信息对话框
	 * 
	 * @param info
	 * 返回类型：void 
	 * 修改人：梁铭显 
	 * 修改时间：2012-9-8
	 */
	public void alert(String info) {
		MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
		dialog.setText("提示");
		dialog.setMessage(info);
		dialog.open();
	}
	
	/**
	 * 
	 * 方法概述：选择对话框
	 * 描述：选择对话框
	 * @param info
	 * 返回类型：boolean
	 * 修改人：梁铭显
	 * 修改时间：2012-9-13
	 */
	public boolean confirm(String info){
		MessageBox dialog=new MessageBox(shell,SWT.OK|SWT.CANCEL|SWT.ICON_QUESTION);
	    dialog.setText("提示");
	    dialog.setMessage(info);
	    
	    if(dialog.open() == SWT.OK){
	    	return true;
	    } else {
	    	return false;
	    }
	}
}
